package com.wxisme.ssm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *多文件夹压缩
 *@author wxisme
 *@time 2015-9-24 下午7:36:06
 */
public final class MultiZip {
	
	public static void ZipFiles(ZipOutputStream out,String path,File... srcFiles) {  
        path = path.replaceAll("\\*", "/");  
        if(!path.endsWith("/")){  
            path+="/";
        }
        byte[] buf = new byte[1024];
        try {  
            for(int i=0;i<srcFiles.length;i++){
            	if(srcFiles[i].getName().endsWith(".zip")) {
            		//防止形成死循环
            		continue;
            	}
                if(srcFiles[i].isDirectory()){  
                    File[] files = srcFiles[i].listFiles();
                    String srcPath = srcFiles[i].getName();
                    srcPath = srcPath.replaceAll("\\*", "/");
                    if(!srcPath.endsWith("/")){
                        srcPath+="/";
                    }
                    out.putNextEntry(new ZipEntry(path+srcPath));
                    ZipFiles(out,path+srcPath,files);
                }
                else{
                    FileInputStream in = new FileInputStream(srcFiles[i]);
                    System.out.println(path + srcFiles[i].getName());  
                    out.putNextEntry(new ZipEntry(path + srcFiles[i].getName()));
                    int len;  
                    while((len=in.read(buf))>0){  
                        out.write(buf,0,len);
                    }  
                    out.closeEntry();
                    in.close();
                }  
            }  
        } catch (Exception e) {
            e.printStackTrace();
        }  
    } 
	
	public static void ZipFiles(File zip,String path,File... srcFiles) throws IOException {
		if(zip.exists()) {
			zip.delete();
		}
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
        ZipFiles(out,path,srcFiles);
        out.close();
    }

}
